home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / HIERSV.PAK / SVRDOC.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  8KB  |  290 lines

  1. // svrdoc.cpp : implementation of the CServerDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13.  
  14. #include "stdafx.h"
  15. #include "hiersvr.h"
  16.  
  17. #include "svrdoc.h"
  18. #include "svritem.h"
  19. #include "svrview.h"
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char BASED_CODE THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. CLIPFORMAT NEAR CServerDoc::m_cfPrivate = NULL;
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CServerDoc
  30.  
  31. IMPLEMENT_DYNCREATE(CServerDoc, COleServerDoc)
  32.  
  33. BEGIN_MESSAGE_MAP(CServerDoc, COleServerDoc)
  34.     //{{AFX_MSG_MAP(CServerDoc)
  35.     ON_COMMAND(ID_OPTIONS_FONT, OnOptionsFont)
  36.     ON_COMMAND(ID_CANCEL_INPLACE, OnCancelInplace)
  37.     //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CServerDoc construction/destruction
  42.  
  43. CServerDoc::CServerDoc()
  44. {
  45.     m_pRoot = CServerNode::CreateRootNode(this);
  46.  
  47.     // determine default font for document
  48.     memset(&m_logfont, 0, sizeof m_logfont);
  49.     m_logfont.lfHeight = -10;
  50.     lstrcpy(m_logfont.lfFaceName, _T("Arial"));
  51.     m_logfont.lfOutPrecision = OUT_TT_PRECIS;
  52.     m_logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  53.     m_logfont.lfQuality = PROOF_QUALITY;
  54.     m_logfont.lfPitchAndFamily = FF_SWISS | VARIABLE_PITCH;
  55.  
  56.     // use default window text color
  57.     m_crText = COLOR_WINDOWTEXT+1;
  58.  
  59.     if (m_cfPrivate == NULL)
  60.     {
  61.         m_cfPrivate = (CLIPFORMAT)
  62. #ifdef _MAC
  63.             ::RegisterClipboardFormat(_T("HIER"));
  64. #else
  65.             ::RegisterClipboardFormat(_T("MFC HierSvr Sample"));
  66. #endif // _MAC
  67.     }
  68. }
  69.  
  70. CServerDoc::~CServerDoc()
  71. {
  72.     delete m_pRoot;     // delete kills child nodes
  73. }
  74.  
  75. void CServerDoc::DeleteContents()
  76. {
  77.     COleServerDoc::DeleteContents();
  78.  
  79.     if (m_pRoot != NULL)
  80.         m_pRoot->InitRootNode();
  81. }
  82.  
  83. BOOL CServerDoc::OnNewDocument()
  84. {
  85.     if (!COleServerDoc::OnNewDocument())
  86.         return FALSE;
  87.  
  88.     // Note: m_pRoot is created in the constructor
  89.     return (m_pRoot != NULL);
  90. }
  91.  
  92. COleServerItem* CServerDoc::OnGetEmbeddedItem()
  93. {
  94.     ASSERT_VALID(m_pRoot);
  95.  
  96.     // allocate embedded item first time requested
  97.     if (m_pRoot->m_pServerItem == NULL)
  98.         m_pRoot->m_pServerItem = new CServerItem(this, m_pRoot);
  99.  
  100.     // and return it
  101.     ASSERT_VALID(m_pRoot->m_pServerItem);
  102.     return m_pRoot->m_pServerItem;
  103. }
  104.  
  105. COleServerItem* CServerDoc::OnGetLinkedItem(LPCTSTR lpszItemName)
  106. {
  107.     ASSERT_VALID(m_pRoot);
  108.  
  109.     // look in current list first
  110.     COleServerItem* pItem = COleServerDoc::OnGetLinkedItem(lpszItemName);
  111.     if (pItem != NULL)
  112.         return pItem;
  113.  
  114.     // look in document itself for an item with that link name
  115.     CString strItemName = lpszItemName;
  116.     CServerNode* pNode = m_pRoot->FindNode(strItemName);
  117.     if (pNode == NULL)
  118.         return NULL;    // node does not exist
  119.  
  120.     ASSERT(pNode->m_pServerItem == NULL);   // should not be connected
  121.     pItem = new CServerItem(this, pNode);
  122.     ASSERT_VALID(pItem);
  123.  
  124.     // return new item that matches lpszItemName
  125.     return pItem;
  126. }
  127.  
  128. CFont* CServerDoc::SelectDocFont(CDC* pDC, CFont& font)
  129. {
  130.     // convert points in m_logfont.lfHeight to logical
  131.     LOGFONT logfont = m_logfont;
  132.     logfont.lfHeight = -::MulDiv(-logfont.lfHeight,
  133.         pDC->GetDeviceCaps(LOGPIXELSY), 72);
  134.  
  135.     // set the text color as appropriate
  136.     COLORREF cr = m_crText;
  137.     if (cr == COLOR_WINDOW+1)
  138.         cr = GetSysColor(COLOR_WINDOW);
  139.     pDC->SetTextColor(m_crText);
  140.  
  141.     // create the font object
  142.     if (!font.CreateFontIndirect(&logfont))
  143.         return NULL;
  144.  
  145.     // select the font
  146.     return pDC->SelectObject(&font);
  147. }
  148.  
  149. int CServerDoc::DrawTree(CDC* pDC, CPoint ptStart, CServerNode* pNodeSel,
  150.     CServerNode* pRoot)
  151. {
  152.     if (pRoot == NULL)
  153.         pRoot = m_pRoot;
  154.     // select correct font for the document
  155.     CFont font;
  156.     CFont* pOldFont = SelectDocFont(pDC, font);
  157.     if (pOldFont == NULL)
  158.         return -1;
  159.  
  160.     // draw the hierachy list
  161.     int iResult = pRoot->DrawTree(pDC, ptStart, pNodeSel);
  162.  
  163.     // restore state of the dc
  164.     pDC->SelectObject(pOldFont);
  165.     return iResult;
  166. }
  167.  
  168. void CServerDoc::CalcBounding(CDC* pDC, CServerNode* pNodeStart,
  169.     CPoint ptStart, CSize& sizeMax)
  170. {
  171.     ASSERT_VALID(pNodeStart);
  172.  
  173.     // select correct font for the document
  174.     CFont font;
  175.     CFont* pOldFont = SelectDocFont(pDC, font);
  176.  
  177.     // calculate the bounding rect
  178.     pNodeStart->CalcBounding(pDC, ptStart, sizeMax);
  179.  
  180.     // restore state of the dc
  181.     if (pOldFont != NULL)
  182.         pDC->SelectObject(pOldFont);
  183. }
  184.  
  185. /////////////////////////////////////////////////////////////////////////////
  186. // CServerDoc serialization
  187.  
  188. // this serializes the OLE Server document as a stand-alone file
  189. void CServerDoc::Serialize(CArchive& ar)
  190. {
  191.     ASSERT(m_pRoot != NULL);
  192.     SerializeFontInfo(ar);
  193.     m_pRoot->Serialize(ar);
  194. }
  195.  
  196. void CServerDoc::SerializeFontInfo(CArchive& ar)
  197. {
  198.     if (ar.IsStoring())
  199.     {
  200.         ar.Write(&m_logfont, sizeof(m_logfont) - sizeof(m_logfont.lfFaceName));
  201.         // lfFaceName is stored as CString so it is UNICODE/ANSI independent
  202.         ar << CString(m_logfont.lfFaceName);
  203.         ar << m_crText;
  204.     }
  205.     else
  206.     {
  207.         ar.Read(&m_logfont, sizeof(m_logfont) - sizeof(m_logfont.lfFaceName));
  208.         // lfFaceName must be read as a CString
  209.         CString strFaceName;
  210.         ar >> strFaceName;
  211.         lstrcpy(m_logfont.lfFaceName, strFaceName);
  212.         ar >> m_crText;
  213.     }
  214. }
  215.  
  216. /////////////////////////////////////////////////////////////////////////////
  217. // CServerDoc in-place editing
  218.  
  219. void CServerDoc::OnSetItemRects(LPCRECT lpPosRect, LPCRECT lpClipRect)
  220. {
  221.     // get first view of document
  222.     POSITION pos = GetFirstViewPosition();
  223.     ASSERT(pos != NULL);
  224.     CServerView* pView = (CServerView*)GetNextView(pos);
  225.     ASSERT_KINDOF(CServerView, pView);
  226.     ASSERT_VALID(pView);
  227.  
  228.     CSize sizeNum(lpPosRect->right - lpPosRect->left,
  229.         lpPosRect->bottom - lpPosRect->top);
  230.     // for denom -- get extent in device
  231.     // create a view dc
  232.     CServerDC dc(pView);
  233.     // set zoom to 100%
  234.     dc.SetViewportExt(CSize(1,1));
  235.     dc.SetWindowExt(CSize(1,1));
  236.     // get extents in device
  237.     CSize sizeDenom = pView->CalcActualItemSize(m_pRoot, &dc);
  238.  
  239.     // notify first view of potential zoom factor change!
  240.     pView->SetZoomFactor(sizeNum, sizeDenom);
  241.     // resize the window
  242.     COleServerDoc::OnSetItemRects(lpPosRect, lpClipRect);
  243.     // set scrollbar state (if necessary)
  244.     pView->SetScrollInfo();
  245. }
  246.  
  247. void CServerDoc::OnOptionsFont()
  248. {
  249.     CClientDC dc(NULL);
  250.     LOGFONT lf = m_logfont;
  251.     lf.lfHeight = -::MulDiv(-lf.lfHeight, dc.GetDeviceCaps(LOGPIXELSY), 72);
  252.     CFontDialog dlg(&lf);
  253.     dlg.m_cf.rgbColors = m_crText;
  254.     if (dlg.DoModal() == IDOK)
  255.     {
  256.         lf.lfHeight = -::MulDiv(-lf.lfHeight, 72, dc.GetDeviceCaps(LOGPIXELSY));
  257.         m_crText = dlg.GetColor();
  258.         m_logfont = lf;
  259.         SetModifiedFlag();
  260.         UpdateAllItems(NULL);
  261.         UpdateAllViews(NULL);
  262.     }
  263. }
  264.  
  265. // Note: both the server and the container should have a keyboard method
  266. //  of deactivating an active in-place item.
  267.  
  268. void CServerDoc::OnCancelInplace()
  269. {
  270.     if (IsInPlaceActive())
  271.         OnDeactivateUI(FALSE);
  272. }
  273.  
  274. /////////////////////////////////////////////////////////////////////////////
  275. // CServerDoc diagnostics
  276.  
  277. #ifdef _DEBUG
  278. void CServerDoc::AssertValid() const
  279. {
  280.     COleServerDoc::AssertValid();
  281. }
  282.  
  283. void CServerDoc::Dump(CDumpContext& dc) const
  284. {
  285.     COleServerDoc::Dump(dc);
  286. }
  287. #endif //_DEBUG
  288.  
  289. /////////////////////////////////////////////////////////////////////////////
  290.